﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;

namespace Jb.Web.Areas.JobSeeker.Controllers
{
    [Authorize(Roles = "Job Seeker,Admin")]
    public class JobController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public JobController()
		{
		}

		public JobController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index(int pageNo = 0)
        {
            return View("AppliedJobs", BuildJobListConfig(pageNo));
        }

		public ActionResult AppliedJobs(int pageNo = 0)
        {
            return View(BuildJobListConfig(pageNo));
        }

        public ActionResult FavoriteJobs()
        {
            //bool getPublishedOnly = String.Compare(WebUtil.GetQueryStringValue("Type"), "P", true) == 0 ? true : false;
            //int pageNo = id.ToInt() > 0 ? id.ToInt() : 1;
            UtilityDAO dao = new UtilityDAO();
            Jb.Domain.DbParameter[] parameters = new[]{
                new Jb.Domain.DbParameter("@JobSeekerID", LoggedInUser.ID)                
            };
            //int totalRecord = 0;
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_FAVORITE_JOBS_BY_JOBSEEKER, parameters, true);
            
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return View(listConfig);
        }

        /// <summary>
        /// Deletes a Job from the JobSeeker's favorite job list
        /// </summary>
        /// <param name="jobID"></param>
        /// <returns>
        /// -2 > If the Favorite job was not found for delete
        /// +1 > Deleted successfully.
        /// </returns>
        [HttpPost]
        public int DeleteJobFromFavorite(int jobID)
        {
            FavoriteJob favorite = _Context.FavoriteJobs.FirstOrDefault(P => P.JobID == jobID && P.UserID == LoggedInUser.ID);
            if (favorite == null)
                return -2;
            else
            {   
                _Context.DeleteObject(favorite);
                _Context.SaveChanges();
                SessionCache.RefreshUserInfo();
                return 1;
            }
        }
		#endregion Action Methods
		
		#region Helper Methods

		/// <summary>
        /// Builds a ListConfig object with a list of Job
        /// </summary>
        /// <returns></returns>
		private ListConfig BuildJobListConfig(int pageNo)
		{
			UtilityDAO dao = new UtilityDAO();
            Jb.Domain.DbParameter[] parameters = new[]{
                new Jb.Domain.DbParameter("@JobSeekerID", LoggedInUser.JobSeekerID)
            };
            int totalRecord = 0;
            PagerConfig pager = new PagerConfig(pageNo <= 0 ? 1 : pageNo, ConfigReader.PageSize, 0);
            DataSet ds = dao.GetPagedData(DbObject.SP.GET_PAGED_JOBAPPLICATIONS_BY_JOBSEEKER, parameters, pager.PageNo, pager.PageSize, out totalRecord, true);
            pager.TotalRecord = totalRecord;
            ListConfig listConfig = new ListConfig(ds.Tables[0], pager);
            return listConfig;
		}

        #endregion			
    }
}


